package com.mingyizhudao.app.view.search;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.utils.Utils;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.view.MYBaseActivity;
import com.mingyizhudao.app.view.home.bean.DisNavsEntity;
import com.mingyizhudao.app.view.search.adpter.SearchMoreAdapter;
import com.mingyizhudao.app.view.search.bean.SearchEntity;

public class SearchMoreActivity extends MYBaseActivity{
	private ArrayList<DisNavsEntity> disNavs;
	private TextView tv_search;
	private EditText et_search;
	private Map<String,SearchEntity> map = new HashMap<String,SearchEntity>();
	private SearchMoreAdapter adapter;
	private ListView listView;
	
	private String type,flg;
	private SearchEntity searchEntity;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		initView();
	}

	@Override
	public void initHeaderView() {
	}
	
	
	private void initData() {
		disNavs = (ArrayList<DisNavsEntity>) getIntent().getSerializableExtra("disNavs");
		searchEntity = (SearchEntity) getIntent().getSerializableExtra("searchEntity");
		type = getIntent().getStringExtra("type");
		flg = getIntent().getStringExtra("flg");
		et_search.setText(flg);
		if ("0".equals(type)) {
			et_search.setHint("请输入医生姓名");
			
		}else if ("1".equals(type)) {
			et_search.setHint("请输入疾病名称");
			
		}else if ("2".equals(type)) {
			et_search.setHint("请输入医院名称");
		}
		
	}
	private void initView(){
		listView = (ListView) findViewById(R.id.list_search);
		et_search = (EditText) findViewById(R.id.et_search);
		initData();
		adapter = new SearchMoreAdapter(this,listView,disNavs,getIntent().getStringExtra(Constants.PARM_ACTION_URL),type);
		listView.setAdapter(adapter);
		adapter.setData(flg ,searchEntity);
		adapter.notifyDataSetChanged();
		
		ImageView img_back =(ImageView) findViewById(R.id.img_back);
		img_back.setImageResource(R.drawable.left_back_gray_icon);
		img_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		
		tv_search = (TextView)findViewById(R.id.tv_search);
		tv_search.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				et_search.setText("");
				adapter.setData("",new SearchEntity());
				adapter.notifyDataSetChanged();
			}
		});
		
		
		
		
		
		et_search.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				String key = arg0+"";
				if(Utils.isStringEmpty(arg0)) return;
				doSeach(key);
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				
			}
		});
	}
	
	private void doSeach(String key){
		try{
			SearchEntity result = map.get(key);
			key = new String(key.getBytes(),"UTF-8");
			if(result == null){
				String flg = URLEncoder.encode(key,"UTF-8");
				String url = Constants.baseURL + "search"+"?name="+flg;
				getHttpResult(url, flg , new TypeToken<QjResult<SearchEntity>>() {});
			}else{
				doChangeView(key);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	@Override
	public void onSuccess(QjResult result, String flg) {
		if(result != null){
			SearchEntity entity = (SearchEntity)result.getResults();
			if(entity != null){
				map.put(URLDecoder.decode(flg), entity);
				doChangeView(URLDecoder.decode(flg));
			}else{
				map.put(URLDecoder.decode(flg), null);
			}
		}
	}
	
	@Override
	public void onError(Exception ex, Object result, String flg) {
		map.put(URLDecoder.decode(flg), null);
	}
	
	@Override
	public void onFailure(QjResult result, String flg) {
		map.put(URLDecoder.decode(flg), null);
	}
	
	private void doChangeView(String flg){
		String value = et_search.getText().toString(); 
		if(value.equals(flg)){
			SearchEntity entity = map.get(flg);
			adapter.setData(flg,entity);
			listView.setAdapter(adapter);
		}
	}
	
	
}
